package com.neusoft.shry.service;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.neusoft.shry.dao.PositionDao;
import com.neusoft.shry.dto.PositionDto;
import com.zero.basic.dao.SqlUpdateBaseDao;
import com.zero.basic.exception.SmsErrorConstant;
import com.zero.basic.exception.SmsRuntimeException;
import com.zero.basic.exception.SmsStatusCode;
import com.zero.basic.model.Pager;
import com.zero.basic.model.type.PositionType;
import com.zero.mms.model.Dep;
import com.zero.mms.model.Employee;
import com.zero.mms.model.Position;
import com.zero.mms.model.SalaryItem;

@Service("positionService")
@Transactional(rollbackFor = Exception.class)
public class PositionServiceImp implements PositionService {

	@Inject
	private PositionDao positionDao;
	
	//get positions by conditions
	@Override
	public Pager<PositionDto> getPositionsByConditions(Integer dep_id,String dep_name,Integer position_id,String position_name,String position_type) throws SmsRuntimeException {
		try {
			if("*".equals(dep_id)||"".equals(dep_id)){
				dep_id = null;
			}
			if("*".equals(dep_name)){
				dep_name = "";
			}
			if("*".equals(position_id)||"".equals(position_id)){
				position_id = null;
			}
			if("*".equals(position_name)){
				position_name = "";
			}
			if("*".equals(position_type)){
				position_type = "";
			}
			Pager<PositionDto> pager = positionDao.getPositionsByConditions(dep_id, dep_name, position_id, position_name, position_type);
			return pager;
		} catch (Exception e) {
			e.printStackTrace();
			throw new SmsRuntimeException(SmsStatusCode.SERVER_ERROR,SmsErrorConstant.WEB_APPLICATION, "服务器错误");
		}
	}

	//add a position
	@Override
	public void addPosition(Position position) throws SmsRuntimeException {
		try{
			if("".equals(position.getPositionName()) || "".equals(position.getPositionType())|| position.getDepId()==null){
				throw new SmsRuntimeException(SmsStatusCode.INVALID_CODE, SmsErrorConstant.INVALID_AUGUMENT, "参数不合法");
			}
			List<PositionDto> positionList = positionDao.getPositionsByConditions(position.getDepId(), null, null, position.getPositionName(), position.getPositionType()).getDatas();
			System.out.println("positionList:"+positionList);
			if(positionList.size()==0){
				positionDao.add(position);
			}
			else{
				throw new SmsRuntimeException(SmsStatusCode.INVALID_CODE,SmsErrorConstant.INVALID_AUGUMENT, "岗位已存在");
			}
		}catch (SmsRuntimeException e) {
			throw e;
		}catch(Exception e){
			e.printStackTrace();
			throw new SmsRuntimeException(SmsStatusCode.SERVER_ERROR,SmsErrorConstant.WEB_APPLICATION, "服务器错误");
		}
		
	}

	//delete a position
	@Override
	public void deletePosition(Integer position_id)
			throws SmsRuntimeException {
		try{
			if("".equals(position_id) || position_id == null){
				throw new SmsRuntimeException(SmsStatusCode.INVALID_CODE,SmsErrorConstant.INVALID_AUGUMENT, "岗位不存在");
			}
			if(positionDao.viewEmpInPosition(position_id).size()==0){
				positionDao.delete(position_id);
			}else{
				throw new SmsRuntimeException(SmsStatusCode.INVALID_CODE,SmsErrorConstant.INVALID_AUGUMENT, "岗位存在员工，不能删除");
			}
		}catch (SmsRuntimeException e) {
			throw e;
		}catch(Exception e){
			e.printStackTrace();
			throw new SmsRuntimeException(SmsStatusCode.SERVER_ERROR,SmsErrorConstant.WEB_APPLICATION, "服务器错误");
		}
	}

	//update a position
	@Override
	public void updatePosition(Integer position_id,String position_name,Integer dep_id,String position_type,Integer position_maxmember) throws SmsRuntimeException {
		try{
			Position position = new Position();
			position.setPositionId(position_id);
			position.setPositionName(position_name);
			position.setDepId(dep_id);
			position.setPositionType(position_type);
			position.setPositionMax(position_maxmember);
			if("".equals(position.getPositionName()) || "".equals(position.getPositionType())|| position.getDepId()==null){
				throw new SmsRuntimeException(SmsStatusCode.INVALID_CODE, SmsErrorConstant.INVALID_AUGUMENT, "参数不合法");
			}
			
			List<PositionDto> positions = positionDao.getPositionsByConditions(dep_id, null, null, position_name, position_type).getDatas();
			
			Position position1 = new Position();
			
			if(positions.size()==0){
				position1=null;
			}else{
				position1.setDepId(positions.get(0).getDepId());
				position1.setPositionId(positions.get(0).getPositionId());
				position1.setPositionMax(positions.get(0).getPositionMax());
				position1.setPositionName(positions.get(0).getPositionName());
				position1.setPositionType(positions.get(0).getPositionType());
			}
			
			System.out.println("lalalal"+position1);
			
			if(position1!=null&&position1.getPositionId()!=position_id){
				throw new SmsRuntimeException(SmsStatusCode.INVALID_CODE, SmsErrorConstant.INVALID_AUGUMENT, "岗位已存在");
			}
			
			positionDao.updatePosition(position);
			
		}catch (SmsRuntimeException e) {
			throw e;
		}catch(Exception e){
			e.printStackTrace();
			throw new SmsRuntimeException(SmsStatusCode.SERVER_ERROR,SmsErrorConstant.WEB_APPLICATION, "服务器错误");
		}
	}

	//view employees in a position
	@Override
	public List<Employee> viewEmployeesInPosition(Integer position_id)
			throws SmsRuntimeException {
		try{
			if("".equals(position_id) || position_id == null){
				throw new SmsRuntimeException(SmsStatusCode.INVALID_CODE,SmsErrorConstant.INVALID_AUGUMENT, "岗位不存在");
			}else{
				return positionDao.viewEmpInPosition(position_id);
			}
		}catch (SmsRuntimeException e) {
			throw e;
		}catch(Exception e){
			e.printStackTrace();
			throw new SmsRuntimeException(SmsStatusCode.SERVER_ERROR,SmsErrorConstant.WEB_APPLICATION, "服务器错误");
		}
	}

	//view a position
	@Override
	public Position viewPosition(Integer position_id)
			throws SmsRuntimeException {
		try{
			if("".equals(position_id) || position_id == null){
				throw new SmsRuntimeException(SmsStatusCode.INVALID_CODE,SmsErrorConstant.INVALID_AUGUMENT, "岗位不存在");
			}else{
				return positionDao.getPositionById(position_id);
			}
		}catch (SmsRuntimeException e) {
			throw e;
		}catch(Exception e){
			e.printStackTrace();
			throw new SmsRuntimeException(SmsStatusCode.SERVER_ERROR,SmsErrorConstant.WEB_APPLICATION, "服务器错误");
		}
	}

	//view department's name of a position
	@Override
	public String viewDepNameOfPosition(Integer position_id)
			throws SmsRuntimeException {
		try{
			if("".equals(position_id) || position_id == null){
				throw new SmsRuntimeException(SmsStatusCode.INVALID_CODE,SmsErrorConstant.INVALID_AUGUMENT, "岗位不存在");
			}else{
				String depName = positionDao.getDepNameByPositionId(position_id);
				return depName;
			}
		}catch (SmsRuntimeException e) {
			throw e;
		}catch(Exception e){
			e.printStackTrace();
			throw new SmsRuntimeException(SmsStatusCode.SERVER_ERROR,SmsErrorConstant.WEB_APPLICATION, "服务器错误");
		}
	}

	//get all deps
	@Override
	public List<Dep> getAllDeps() throws SmsRuntimeException {
		try{
			return positionDao.getAllDeps();
		}catch(Exception e){
			e.printStackTrace();
			throw new SmsRuntimeException(SmsStatusCode.SERVER_ERROR,SmsErrorConstant.WEB_APPLICATION, "服务器错误");
		}
	}

	//get all positions
	@Override
	public List<Position> getAllPositions() throws SmsRuntimeException {
		try{
			return positionDao.getAllPositions();
		}catch(Exception e){
			e.printStackTrace();
			throw new SmsRuntimeException(SmsStatusCode.SERVER_ERROR,SmsErrorConstant.WEB_APPLICATION, "服务器错误");
		}
	}

	
		
}
